package com.lbiuse.wx.bean.pay;

import com.lbiuse.wx.bean.Status;
import com.lbiuse.wx.util.MD5Util;
import com.lbiuse.wx.util.MapUtil;
import com.lbiuse.wx.util.WechatConfigUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.TreeMap;

/**
 * Created by Administrator on 2015/12/4.
 */
@Data
@Slf4j
public class WechatOrderQueryReulst extends Status {
    private String return_code;//
    private String return_msg;//
    private String appid;//
    private String mch_id;//
    private String nonce_str;//
    private String sign;//

    private String result_code;//
    private String err_code;//
    private String err_code_des;//

    private String device_info;//
    private String openid;//
    private String is_subscribe;//
    private String trade_type;//
    private String trade_state;//
    private String total_fee;//
    private String fee_type;//
    private String cash_fee;//
    private String cash_fee_type;//
    private String coupon_fee;//
    private String coupon_count;//
    private String coupon_batch_id_$n;//
    private String coupon_id_$n;//
    private String coupon_fee_$n;//
    private String transaction_id;//
    private String out_trade_no;//
    private String attach;//
    private String time_end;//
    private String trade_state_desc;//

    public boolean checkSign() throws Exception {


        TreeMap<String, String> treeMap = MapUtil.bean2TreeMap(this);
        treeMap.remove("errcode");
        treeMap.remove("errmsg");
        treeMap.remove("sign");

        StringBuilder sb = new StringBuilder();
        for (String key : treeMap.keySet()) {
            if (StringUtils.isNotEmpty( treeMap.get(key))) {
                sb.append(key).append("=").append(treeMap.get(key)).append("&");
            }
        }
        sb.append("key=" + WechatConfigUtil.getShopSecrect());
        log.debug(sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), "utf-8").toUpperCase();
        if (sign.equals(this.sign)) {
            return true;
        } else {
            log.debug("wechatOrderQuery sign error :{}", this);
            return false;
        }
    }
}
